﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WpfApp6.ViewModels;

namespace WpfApp6
{
    /// <summary>
    /// MyBindList.xaml 的交互逻辑
    /// </summary>
    public partial class MyBindList : Window
    {
        public MyBindList()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if(DataContext is BindListViewModel model)
            {
                PersonViewModel person = new PersonViewModel()
                {
                    Name=txtName.Text,
                    Age=int.Parse(txtAge.Text),
                    Address=txtAddress.Text,
                    Job=txtJob.Text
                };
                model.Persons.Add(person);
            }
        }

        // <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            if(myList.SelectedItem is PersonViewModel person)
            {
                var model = DataContext as BindListViewModel;
                model.Persons.Remove(person);
            }
        }
    }
}
